package com.example.demo.service;

import com.example.demo.dao.SysUserDao;
import com.example.demo.entity.SysUser;
import com.example.demo.utils.JwtUserDetails;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;


/**
 * @Author: Administrator
 * @Date: 2019/3/27
 * @Description:
 */
@Service
@Log4j2
public class JwtUserDetailService implements UserDetailsService {
    @Autowired
    SysUserDao sysUserDao;
    /**
     * @Author: zengzr
     * @Date: 2019/3/29
     * @Description: 查询缓存
     * @Cacheable 缓存
     */
    @Override
    @Cacheable(value = "auth",key = "#userName")
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        SysUser sysUser = null;
        log.info("没有使用缓存哟");
        try {
             sysUser = sysUserDao.findSysUserByUserName(userName);
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return  new JwtUserDetails(sysUser.getId(),sysUser.getUserName(),sysUser.getUserPwd(), sysUser.getRoles());
    }

}
